<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>登记退还</span>
          </div>
          <div class="save_button">
            <div v-if="review" class="buttonBox">
              <el-button type="success" icon="el-icon-check" size="mini" @click="reviewSuccess">确认归还</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" :inline="true" :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="status">
                <div>状态</div>
                <el-select v-model="data.status" size="mini" disabled>
                  <el-option v-for="(item, index) in handBackStatus" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="apply_admin_id">
                <div>申请人</div>
                <v-select v-model="data.apply_admin_id" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="workorder_id">
                <div>维修单</div>
                <v-select v-if="data.status === 0" v-model="data.workorder_id" action="cmList" :query="{'category': 1, 'status': 6, 'add_field': 'coding'}" />
                <v-select v-if="data.status === 1" v-model="data.workorder_id" action="cmList" :disabled="!review" />
              </el-form-item>
            </el-col>
            <div v-if="data.verify_admin">
              <el-col :xl="3" :lg="4" :md="4" :sm="5">
                <el-form-item>
                  <div>验证人员</div>
                  <el-input v-model="data.verify_admin.name" :disabled="true" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="4" :sm="5">
                <el-form-item>
                  <div>验证时间</div>
                  <el-date-picker v-model="data.verify_date" type="date" size="mini" :disabled="true" />
                </el-form-item>
              </el-col>
            </div>
            <el-col :xl="3" :lg="4" :md="4" :sm="5">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="详细">
            <el-table :data="list">
              <el-table-column prop="parts.name" label="备件" min-width="23%" />
              <el-table-column prop="parts.store_locations.name" label="库位" min-width="23%" />
              <el-table-column prop="number" label="数量" min-width="18%" />
              <el-table-column prop="notice" label="备注" min-width="37%" />
              <el-table-column prop="" label="操作" min-width="9%">
                <template scope="scope">
                  <div v-if="review">
                    <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  </div>
                  <div v-else>
                    -
                  </div>
                </template>
              </el-table-column>
              <div v-if="review" slot="append">
                <el-form ref="parts_form" :model="parts" :inline="true" class="hf_form append_other_admin" :rules="partsRules">
                  <el-row :gutter="10">
                    <el-col :xl="5" :lg="5" :md="5" :sm="5">
                      <el-form-item prop="part_id">
                        <v-select v-model="parts.part_id" action="partsList" :query="{workorder_id:data.workorder_id}" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="5" :lg="5" :md="5" :sm="5">
                      <el-form-item>
                        <el-input v-model="parts.store" disabled placeholder size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="3" :lg="4" :md="4" :sm="4">
                      <el-form-item prop="number">
                        <el-input v-model="parts.number" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="8" :lg="8" :md="8" :sm="8">
                      <el-form-item prop="notice">
                        <el-input v-model="parts.notice" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :xl="2" :lg="2" :md="2" :sm="2">
                      <el-form-item>
                        <el-button icon="el-icon-upload2" size="mini" @click="append" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :xl="3" :lg="4" :md="4" :sm="5">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                  </div>
                </div>
              </el-col>
              <el-col :xl="21" :lg="20" :md="20" :sm="19">
                <Tinymce id="content" v-model="data.content" />
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/stock/handBack'
import VSelect from '@/components/select'
import { handBackStatus } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendParts', 'removeParts', 'goUrl'],
  components: {
    VSelect,
    Tinymce
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        },
        'verify_admin': {
          'name': ''
        }
      },
      review: false,
      list: [],
      parts: {},
      handBackStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入退还单名称',
            trigger: 'blur'
          }
        ],
        apply_admin_id: [
          {
            required: true,
            message: '请选择申请人',
            trigger: 'change'
          }
        ],
        workorder_id: [
          {
            required: true,
            message: '请选择维修单',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      partsRules: {
        part_id: [
          {
            required: true,
            message: '请选择备件',
            trigger: 'change'
          }
        ],
        number: [
          {
            required: true,
            message: '请填写数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.error_code === 47404) {
        this.$alert('您请求的退还单不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.list = response.data.parts_list
      if (response.data.status === 0) {
        this.review = true
      }
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.parts_form
      const parts_data = {
        'part_id': this.parts.part_id,
        'number': this.parts.number,
        'notice': this.parts.notice,
        'handback_id': this.$route.params.id
      }
      const errorCode = 48004
      const errorMessage = '该备件已存在'
      this.appendParts(API, thisForm, parts_data, errorCode, errorMessage)
    },
    remove(id) {
      const parts_data = {
        'id': id
      }
      this.removeParts(API, id, parts_data)
    },
    reviewSuccess() {
      const reviewData = {
        'status': 1,
        'id': this.data.id,
        'appear_id': this.data.appear_id
      }
      API.review(reviewData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('归还完成！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    goBack() {
      this.goUrl('/stock/back')
    }
  }
}
</script>
